package com.admin;








import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.Constants;
import com.admin.RoleAction;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;




public class RoleAction extends RoleService {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6323809774569843034L;
	private Role role;
	private List<Role> rol;
	private String rolename;
	private String usercode;
	private String oldrolename;
	private String comid;
	private String roleids;
	private int Roleid;
	private String Xgzd;
	private String Value;
	private String rolecompany;
	private String rolemenu;
	private String ids;
	public String execute() {
		
			return "success";
	}
	
	/*//显示角色表格
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String dotoshowroletabe(){
		rol = getrole();
		ajaxJsontable(rol);	
		return null;
	}*/
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void selectroles() {
		Map params = new HashMap();
		Role role =(Role)session.get(Constants.USER_ROLES);
		String tj="";
		String nr="";
		if(role.getRoleid()!=1) {
			Company com =(Company)session.get(Constants.USER_COMPANYS);
			params.put("jointable", " join \"Role_Company\"  on \"Role\".\"Roleid\"= \"Role_Company\".\"Roleid\"");
			
			tj=" and \"Comid\" ="+com.getComid();
			nr=",\"Comid\"";
		}
		
		
		params.put("table", "\"Role\"");
		params.put("where", "\"Stutas\"!=9999"+tj);
		params.put("nr", "distinct \"Role\".\"Roleid\" as \"key\",\"Rolename\" as \"value\",\"Stutas\""+nr);
		params.put("px", "\"Role\".\"Roleid\"");
		List<LinkedHashMap<String, String>>	list =selecttable(params);
		ajaxJson(list);
		
		
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void xgrolecom() {
		if(Roleid==0 || ids==null) {
			message="数据丢失！";
			savelog(message);
			ajaxJson(message);
		}else {
			Map params = new HashMap();
			params.put("table", "\"Role_Company\"");
			String where="\"Roleid\"="+Roleid;
			params.put("where", where);
			deletetable(params);
			User loginuser = (User)session.get(Constants.USER);
			String zdm="\"Roleid\",\"Comid\",\"Createuserid\"";
			params.put("zdm", zdm);
			String[]  comidlist=ids.split(",");
			for(int i=0;i<comidlist.length;i++){ {
				String nr=Roleid+","+comidlist[i]+","+loginuser.getUserid();
				params.put("nr", nr);
			    inserttable(params);
			}
			
		}
			message="修改成功";
			savelog(message);
			ajaxJson(message);
		}
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void xgrolemenu() {
		if(Roleid==0 || ids==null) {
			message="数据丢失！";
			savelog(message);
			ajaxJson(message);
		}else {
			Map params = new HashMap();
			params.put("table", "\"Role_Menu\"");
			String where="\"Roleid\"="+Roleid;
			params.put("where", where);
			deletetable(params);
			User loginuser = (User)session.get(Constants.USER);
			String zdm="\"Roleid\",\"Menuid\",\"Createuserid\"";
			params.put("zdm", zdm);
			String[]  menuidlist=ids.split(",");
			for(int i=0;i<menuidlist.length;i++){ {
				String nr=Roleid+","+menuidlist[i]+","+loginuser.getUserid();
				params.put("nr", nr);
			    inserttable(params);
			}
			
		}
			message="修改成功";
			savelog(message);
			ajaxJson(message);
		}
	}
	
	
	//显示角色表格
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void dotoshowroletabe(){
		Map params = new HashMap();
		params.put("rolename", rolename);
		rol = getroles(params);
		ajaxJsontable(rol);	
		return;
		
		
		/*Map params = new HashMap();
		String like="1=1";
		if(rolename!=null) {
			if(!rolename.equals("")) {
				
				like+=" and \"Rolename\" like '%"+rolename+"%'";
			}
			
		}
		params.put("Stutas", "0");
		params.put("like", like);
		List<LinkedHashMap<String, String>>	list = getroles(params);
		ajaxJsontable(list);	
		return null;*/
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String rolemod() {
		if(Roleid==0) {
			message="数据丢失！";
			
			return null;
		}else {
			
			Map params = new HashMap();
			params.put("nr", "\"Roleid\",string_agg(cast (\"Comname\" as VARCHAR),',') as comids");
			params.put("table", "\"Role_Company\"");
			params.put("px", "\"Roleid\" desc");
			params.put("group", "group by \"Roleid\"");
			params.put("Roleid", Roleid);
			params.put("jointable", " join \"Company\" on \"Role_Company\".\"Comid\"=\"Company\".\"Comid\"");
			List<LinkedHashMap<String, String>>	list = getlist(params);
			JsonParser parser = new JsonParser();
			String strs;
			JsonElement jsonEl;
			JsonObject jsonObj = null;
			if(list.size()>0) {
	
	 strs = gson.toJson(list.get(0));
	
	 jsonEl = parser.parse(strs);

	
	jsonObj = jsonEl.getAsJsonObject();//转换成Json对象
	rolecompany=jsonObj.get("comids").getAsString();//status节点

}			params.put("jointable", "");
			params.put("nr", "\"Roleid\",string_agg(cast (\"Menuid\" as VARCHAR),',') as menus");
			params.put("table", "\"Role_Menu\"");
			list = getlist(params);
			
			if(list.size()>0) {
				
				strs = gson.toJson(list.get(0));
				jsonEl = parser.parse(strs);
	            jsonObj = null;
				jsonObj = jsonEl.getAsJsonObject();//转换成Json对象
				rolemenu=jsonObj.get("menus").getAsString();//status节点
				
				
			}
			
		}
		
		
		return "success";
	}
	
	
	/*//新增角色信息
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String rolenew(){
		Map params =new HashMap();
		params.put("rolename", rolename);
		int result = rolemod(params);
		
		return message;
	}*/
		
		
		public void savenewRolename() {
			if(role==null) {
				message="数据丢失！";
				
			}else {
				User loginuser = (User)session.get(Constants.USER);
				role.setCreateuserid(loginuser.getUserid());
				
				int repeat = isRolerepeat(role.getRolename());
				if(repeat>0){//重复
					message="角色名称重复！";
				}else{
				message=saverole(role);
				}
				
			}
			savelog(message);
			ajaxJson(message);
			return;
			
		}
		
		
	
	//修改角色信息

			@SuppressWarnings({ "rawtypes", "unchecked" })
			public void xgrole(){
				if(Roleid==0) {
					message="-1";
					ajaxJson(message);
					savelog("参数丢失");
					return;
				}
				if(Xgzd.equals("Rolename")) {
					int repeat = isRolerepeat(Value);
					if(repeat>0) {
						message="1";
						ajaxJson(message);
						savelog("角色名称重复，修改失败");
						return;
					}
				}
				User loginuser = (User)session.get(Constants.USER);
				
				Map params = new HashMap();
				params.put("Keyidvalue", Roleid);
				params.put("Keyid", "Roleid");
				params.put("Xgzd",Xgzd);
				params.put("Value",Value);
				params.put("nr", "*");
				int result =rolemod(params);
				String msg="";
				if(result==0){//修改失败   数据库保存失败
					message="2";
					msg="数据库保存异常";
				}else{//修改成功
					params.put("Xgzd","Xguserid");
					params.put("Value",loginuser.getUserid());
					rolemod(params);
					msg="操作成功";
				}
				savelog(msg);
				ajaxJson(message);
			}
			
			public void roledel(){
				if(roleids == null){
					message = "参数缺失!";
				}
				int result=0;
				result=delectroleids(roleids);
				if(result!=0){
					message="删除成功!";
				}else{
					message="删除失败!";
				}
				ajaxJson(message);
			}

			
	//下拉显示角色
	public void selectRole(){//传入了
		List<Role> list = selectRoles();
		ajaxJson(list);		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void swroles(){
		int result1;
		int result2;		
		Map map = new HashMap();
		map.put("Usercode", usercode);
		String userid = selectUserid(map);
		Map map1 = new HashMap();
		map1.put("Userid", userid);
		map1.put("oldRolename",oldrolename);
		Map map2 = new HashMap();
		map2.put("Userid", userid);
		map2.put("Rolename",rolename);
		
		String msg="";
		if(oldrolename.equals(rolename)){
			message ="3";//角色相同无需切换
			msg="未切换";
			
		}else{
			result1=updateoldRole(map1);
			result2=updateswitchRole(map2);
			if(result1!=0&&result2!=0){
				message ="1";//角色切换成功!
				msg="操作成功";
				
			}else{
				message ="2";//切换失败,数据库未修改成功!
				msg="操作失败";
				
			}
		}
		savelog(msg);
		ajaxJson(message);
	}
	
	
	//新增用户中的角色列表
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  void getrolelist(){
		Map map = new HashMap();
		map.put("Comid", comid);
		List<Role> list = getrolelists(map);
		ajaxJsontable(list);
	}
	
	
	
	
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<Role> getRol() {
		return rol;
	}

	public void setRol(List<Role> rol) {
		this.rol = rol;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}


	public String getUsercode() {
		return usercode;
	}

	public void setUsercode(String usercode) {
		this.usercode = usercode;
	}

	public String getOldrolename() {
		return oldrolename;
	}

	public void setOldrolename(String oldrolename) {
		this.oldrolename = oldrolename;
	}


	public String getRoleids() {
		return roleids;
	}

	public void setRoleids(String roleids) {
		this.roleids = roleids;
	}

	public int getRoleid() {
		return Roleid;
	}

	public void setRoleid(int roleid) {
		Roleid = roleid;
	}

	public String getXgzd() {
		return Xgzd;
	}

	public void setXgzd(String xgzd) {
		Xgzd = xgzd;
	}

	public String getValue() {
		return Value;
	}

	public void setValue(String value) {
		Value = value;
	}

	public String getRolecompany() {
		return rolecompany;
	}

	public void setRolecompany(String rolecompany) {
		this.rolecompany = rolecompany;
	}

	public String getRolemenu() {
		return rolemenu;
	}

	public void setRolemenu(String rolemenu) {
		this.rolemenu = rolemenu;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getComid() {
		return comid;
	}

	public void setComid(String comid) {
		this.comid = comid;
	}



	
	
	
}